﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

namespace GameToolkit
{
    /// <summary>
    /// 立方体寻路区域
    /// </summary>
    [RequireComponent(typeof(BoxCollider))]
    [AddComponentMenu("Navigation/Runtime NavMeshBox")]
    public class NavMeshBoxRT : NavMeshSource
    {
        BoxCollider mBox;
        public override Bounds localBounds
        {
            get
            {
                if (mBox == null)
                    mBox = GetComponent<BoxCollider>();
                return new Bounds(mBox.center, mBox.size);
            }
        }

        public override void GetBuildSource(int areaType, ICollection<NavMeshBuildSource> sources)
        {
            BoxCollider box;
            if (!TryGetComponent(out box))
                return;
            var src = new NavMeshBuildSource();
            src.shape = NavMeshBuildSourceShape.Box;
            src.size = box.size;
            src.area = this.areaType == 0 ? areaType : this.areaType;
            //src.sourceObject = b;
            src.transform = transform.localToWorldMatrix * Matrix4x4.Translate(box.center);
            sources.Add(src);
            //return src;
        }

//#if UNITY_EDITOR

//        private void OnDrawGizmosSelected()
//        {
//            Gizmos.matrix = transform.localToWorldMatrix;
//            Gizmos.color = Color.gray * 0.8f;
//            var bd = localBounds;
//            Gizmos.DrawWireCube(bd.center, bd.size);
//        }
//#endif
    }
}